<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Menu extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
        if(!$this->tank_auth->is_logged_in())
            redirect('administrator/auth');
        $this->load->model('menus','men');
        $this->load->model('tank_auth/roles','rol');
    }

    /**
     * Default function
     */
    public function index()
    {
        //redirect('administrator/menu/menu');
    }
    
    public function menus($act=false){
        $post= $this->input->post(null);
        $this->_layout['action'] = $this->uri->segment(4);
        $this->_layout['menu_id'] = $this->uri->segment(5);
        
        $menus = $this->men->get_menu();
        $this->assign('menus',$menus);
        $this->assign('action','');
        
        if($act)
        {   
            $this->_layout['roles']=$this->rol->get_roles();
            $this->_layout['menus_list'] = $menus;
            if($act=="save"){
                if($post['menu_name'] ==""){
                            _set_error_message('....');
                            redirect('administrator/menu/menus');
                        }
                switch ($post['action']) {
                    case 'create':                            
                        $menuname = $this->_global->_serialize(array($this->session->userdata('lan')=>array('menu_name'=>$post['cat_name'])));
                        $menutype = $this->_global->_serialize(array($this->session->userdata('lan')=>array('menu_type'=>$post['menu_type'])));                        
                        $menutitle = $this->_global->_serialize(array($this->session->userdata('lan')=>array('menu_title'=>$post['menu_title'])));
                        $menualias = $this->_global->_serialize(array($this->session->userdata('lan')=>array('menu_alias'=>_set_alias($post['menu_alias']))));
                        $type = $this->_global->_serialize(array($this->session->userdata('lan')=>array('type'=>$post['type'])));
                        $params = $this->_global->_serialize(array($this->session->userdata('lan')=>array('params'=>$post['params'])));
                        $fields = array('menu_type'=>$menutype
                                    ,'menu_name'=>$menuname
                                    ,'menu_title'=>$menutitle
                                    ,'menu_alias'=>$menualias
                                    ,'type'=>$type
                                    ,'parent_id'=>$post['parent_id']
                                    ,'ordering'=>$post['ordering']
                                    ,'ishomepage'=>$post['ishomepage']
                                    ,'access'=>$post['access']
                                    ,'params'=>$params);
                        $rs = $this->men->add_menu($fields);
                        if($rs>0)
                            _set_success_message('Save successful');
                        else
                            _set_error_message('....');
                        redirect('administrator/menu/menus');
                        break;
                    case 'edit':
                        $menu = $this->men->get_menu(array('menu_id'=>$post['menu_id']));
                        $menuname = $this->_global->_unserialize($menu->menu_name);
                        $menutype = $this->_global->_unserialize($menu->menu_type);
                        $menutitle = $this->_global->_unserialize($menu->menu_title);
                        $menualias = $this->_global->_unserialize($menu->menu_alias);
                        $type = $this->_global->_unserialize($menu->type);
                        $params = $this->_global->_unserialize($menu->params);                                                
                        if(!isset($menuname[$this->session->userdata('lan')]))
                        {
                            //@TODO
                        }else
                            $menuname[$this->session->userdata('lan')]['menu_name']=$post['menu_name'];
                        if(!isset($menutype[$this->session->userdata('lan')]))
                        {
                            //@TODO
                        }else
                            $menutype[$this->session->userdata('lan')]['menu_type']=$post['menu_type'];
                        if(!isset($menutitle[$this->session->userdata('lan')]))
                        {
                           //@TODO
                        }else
                            $menutitle[$this->session->userdata('lan')]['menu_title']=$post['menu_title'];
                        if(!isset($menualias[$this->session->userdata('lan')]))
                        {
                            //@TODO
                        }else
                            $menualias[$this->session->userdata('lan')]['menu_alias']=_set_alias($post['menu_alias']);
                        if(!isset($type[$this->session->userdata('lan')]))
                        {
                            //@TODO
                        }else
                            $type[$this->session->userdata('lan')]['type']=$post['type'];    
                        if(!isset($params[$this->session->userdata('lan')]))
                        {
                            //@TODO
                        }else
                            $params[$this->session->userdata('lan')]['params']=$post['params'];
                        $menuname = $this->_global->_serialize($menuname);
                        $menutype=$this->_global->_serialize($menutype);
                        $menutitle=$this->_global->_serialize($menutitle);
                        $menualias=$this->_global->_serialize($menualias);
                        $type=$this->_global->_serialize($type);
                        $params = $this->_global->_serialize($params);
                        $rs = $this->cat->update_category(array('menu_type'=>$menutype
                                    ,'menu_name'=>$menuname
                                    ,'menu_title'=>$menutitle
                                    ,'menu_alias'=>$menualias
                                    ,'type'=>$type
                                    ,'parent_id'=>$post['parent_id']
                                    ,'ordering'=>$post['ordering']
                                    ,'ishomepage'=>$post['ishomepage']
                                    ,'access'=>$post['access']
                                    ,'params'=>$params),array('menu_id'=>$post['menu_id']));                        
                        
                        if($rs>0)
                            _set_success_message('Save successful');
                        else
                            _set_error_message('....');
                        redirect('administrator/menu/menus');
                        break;
                }
                
            }elseif($act=="delete"){
                if(!is_numeric($this->_layout['menu_id']) || !$this->_layout['menu_id']){
                    _set_error_message('Cant delete this menu');
                    redirect('administrator/menu/menus');
                }
                $rs = $this->men->delete_menu($this->_layout['menu_id']);
                if($rs>0){
                    _set_success_message('Deleted...');
                }else
                    _set_error_message('Cant delete this menu');
                redirect('administrator/menu/menus');
            }elseif($act=="edit"){
                $this->_layout['menus'] = $this->men->get_menu(array('menu_id'=>$this->_layout['menu_id']));
            }elseif($act=="create"){
                //@TODO
                
            }
            else show_404();
            $form   = $this->load->view('layouts/form/menu',$this->_layout,true);
            $this->assign('action',$form);
        }
    }
    
    
}